import unittest
import requests
# 导入mysql
from unit__001.unit_06.Mysql import Mysql


class WeiDu(unittest.TestCase):
    def setUp(self) -> None:
        print('开始')
        # 初始化数据库
        self.db = Mysql(host='10.59.9.19', user='yidongceshi', password='Yd123456!', database='movie')

    def tearDown(self) -> None:
        print('结束')

    # 登录
    sessionId = None
    userId = None
    nickName = None
    email = None

    def test_001(self):
        r1 = requests.post(url='http://10.59.9.18/movieApi/user/v2/login',
                           params={'email': '3601552946@qq.com', 'pwd': 'eWLPHopE945d2ivttHaQTQ=='})
        print(r1.json())
        # 提取响应中的sessionId、userId
        WeiDu.sessionId = r1.json()['result']['sessionId']
        WeiDu.userId = r1.json()['result']['userId']
        print(WeiDu.userId)
        # 数据库断言1 对比响应中的nickName和数据库中的nickName是否一致
        WeiDu.nickName = r1.json()['result']['userInfo']['nickName']
        result1 = self.db.chaxun(f'select nick_name from user where id={str(WeiDu.userId)}')
        self.assertEqual(WeiDu.nickName, result1[0][0])
        # 数据库断言2 对比响应中的email和数据库中的email是否一致
        WeiDu.email = r1.json()['result']['userInfo']['email']
        result2 = self.db.chaxun(f'select email from user where id={str(WeiDu.userId)}')
        self.assertEqual(WeiDu.email, result2[0][0])
        # 数据库断言3 对比响应中的sex和数据库中的sex是否一致
        WeiDu.sex = r1.json()['result']['userInfo']['sex']
        result3 = self.db.chaxun(f'select sex from user where id={str(WeiDu.userId)}')
        self.assertEqual(WeiDu.sex, result3[0][0])

    # 查询热门电影
    movieId = None
    director = None
    dyname= None
    def test_002(self):
        r2 = requests.get(url='http://10.59.9.18/movieApi/movie/v2/findHotMovieList',
                          params={'page': 1, 'count': 5})
        print(r2.json())
        # 找打热门电影id
        WeiDu.movieId = r2.json()['result'][1]['movieId']
        print(WeiDu.movieId)
        # 数据库断言1-江湖儿女 对比响应中的电影名称是否在数据库中存在
        WeiDu.dyname = r2.json()['result'][1]['name']
        result1 = self.db.chaxun(f'select name from movies where id={str(WeiDu.movieId)}')
        self.assertTrue(WeiDu.dyname == result1[0][0])
        # 数据库断言2-贾樟柯 对比响应中的director的值是否和数据库中的一致
        WeiDu.director = r2.json()['result'][1]['director']
        result2 = self.db.chaxun(f'select director from movies where id={str(WeiDu.movieId)}')
        self.assertEqual(WeiDu.director,result2[0][0])
        # 数据库断言3-8.6 对比响应中对应的电影名称的价格，是否在数据库中对应的电影名的价格一致
        WeiDu.score = r2.json()['result'][1]['score']
        result2 = self.db.chaxun(f'select score from movies where id={str(WeiDu.movieId)}')
        self.assertEqual(WeiDu.score, result2[0][0])

    # 查询区域列表
    regionId = None
    regionName = None
    regionId1= None
    regionName1 = None
    def test_003(self):
        r3 = requests.get(url='http://10.59.9.18/movieApi/tool/v2/findRegionList')
        print(r3.json())
        WeiDu.regionId = r3.json()['result'][6]['regionId']
        # 数据库断言1-海淀区 根据regionId去数据库中查询regionName 对比响应中的和数据库中的是否一致
        WeiDu.regionName = r3.json()['result'][6]['regionName']
        result1 = self.db.chaxun(f'select region_name from region where id={str(WeiDu.regionId)}')
        self.assertEqual(WeiDu.regionName, result1[0][0])
        # 数据库断言2-1001 根据regionName值去数据库中查询regionId值 对比响应中的和数据库中的是否一致
        WeiDu.regionId1 = r3.json()['result'][0]['regionId']
        result1 = self.db.chaxun(f'select region_name from region where id={str(WeiDu.regionId)}')
        self.assertEqual(WeiDu.regionName, result1[0][0])



    # 根据电影id,区域id 查询播放影院信息
    cinemaId = None
    address = None
    name=None
    logo=None
    def test_004(self):
        r4 = requests.get(url='http://10.59.9.18/movieApi/movie/v2/findCinemasInfoByRegion',
                          params={'movieId': WeiDu.movieId, 'regionId': WeiDu.regionId,
                                  'page': 1, 'count': 5})
        print(r4.json())
        # 提取影院Id
        WeiDu.cinemaId = r4.json()['result'][4]['cinemaId']
        print(WeiDu.cinemaId)
        self.assertTrue('复兴路69号五棵松卓展时代百货五层东侧', WeiDu.address)
        # 数据库断言1-我的最新签名子 对比响应中提取的name和数据库中的name的是否一致
        WeiDu.name = r4.json()['result'][4]['name']
        result1 = self.db.chaxun(f'select name from cinemas where id={str(WeiDu.cinemaId)}')
        self.assertEqual(WeiDu.name, result1[0][0])
        # 数据库断言2-中关村广场购物中心津乐汇三层（鼎好一期西侧）
        # 对比响应中提取的name和数据库中的name的是否一致
        WeiDu.address = r4.json()['result'][4]['address']
        result2 = self.db.chaxun(f'select address from cinemas where id={str(WeiDu.cinemaId)}')
        self.assertEqual(WeiDu.address, result2[0][0])
        # 数据库断言3-'logo' 对比响应中提取的logo和数据库中的logo的是否一致
        WeiDu.logo = r4.json()['result'][4]['logo']
        result3 = self.db.chaxun(f'select logo from cinemas where id={str(WeiDu.cinemaId)}')
        self.assertEqual(WeiDu.logo, result3[0][0])


    # 根据电影ID和影院ID查询电影排期列表
    hallId = None
    pqid = None
    screeningHall=None
    endTime=None
    def test_005(self):
        r5 = requests.get(url='http://10.59.9.18/movieApi/movie/v2/findMovieSchedule',
                          params={'movieId': WeiDu.movieId, 'cinemaId': WeiDu.cinemaId})
        print(r5.json())
        WeiDu.hallId = r5.json()['result'][0]['hallId']
        # 提取排期id
        WeiDu.pqid = r5.json()['result'][0]['id']
        # 数据库断言1-2号激光厅 对比响应中提取的screeningHall和数据库中的screening_hall的是否一致
        WeiDu.screeningHall = r5.json()['result'][0]['screeningHall']
        result1 = self.db.chaxun(f'select screening_hall from movie_schedule where hall_id={str(WeiDu.hallId)}')
        self.assertEqual(WeiDu.screeningHall, result1[0][0])

    # 影院评论
    def test_006(self):
        header = {'userId':str(WeiDu.userId),'sessionId': str(WeiDu.sessionId)}
        r6 = requests.post(url='http://10.59.9.18/movieApi/cinema/v1/verify/cinemaComment',
                          headers=header,
                          json={'cinemaId': WeiDu.cinemaId, 'commentContent': '很棒'})
        print(r6.json())





if __name__ == '__main__':
    unittest.main()
